package com.github.prontera.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.prontera.ApiVersion;
import com.github.prontera.RequestLogging;
import com.github.prontera.Shift;
import com.github.prontera.StatusCode;
import com.github.prontera.common.filter.CurrentUserId;
import com.github.prontera.domain.ClientBankCard;
import com.github.prontera.domain.enums.ClientBankCardStatusEnum;
import com.github.prontera.model.response.ObjectDataResponse;
import com.github.prontera.service.ClientBankCardService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;


/**
 * 银行卡
 */
@RestController
@Api(value = "银行卡")
@RequestMapping(value = ApiVersion.API_V1_PREFIX+"/bankCard")
public class BankCardController{

	@Autowired
	private ClientBankCardService clientBankCardService;
	
	/**
	 * 我的银行卡列表
	 */
	
	@RequestMapping(value = "find", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "我的银行卡列表" , notes = "我的银行卡列表")
	public ObjectDataResponse<List<ClientBankCard>> find(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId,@RequestBody ClientBankCard clientBankCard){
		clientBankCard.setClientId(clientId);
		clientBankCard.setStatus(ClientBankCardStatusEnum.ACTIVITY);
		List<ClientBankCard> datas=clientBankCardService.find(clientBankCard);
		return new ObjectDataResponse<List<ClientBankCard>>(datas);
	}
	
	/**
	 * 添加银行卡
	 */
	@RequestMapping(value = "insertCard", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "添加银行卡" , notes = "添加银行卡")
	public ObjectDataResponse<Boolean> insertCard(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId,@RequestBody ClientBankCard clientBankCard){
		clientBankCard.setClientId(clientId);
		if(clientBankCard.getCardNo()==null) {
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "cardNo必传!");
		}
		if(clientBankCard.getCardType()==null) {
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "cardType必传!");
		}
		boolean success=clientBankCardService.insertCard(clientBankCard);
		return new ObjectDataResponse<Boolean>(success);
	}
	
	/**
	 * 设置默认卡
	 */
	@RequestMapping(value = "defaultCard", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "设置默认卡" , notes = "设置默认卡")
	public ObjectDataResponse<Boolean> defaultCard(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId,@RequestBody String cardNo){
		ClientBankCard clientBankCard=new ClientBankCard();
		clientBankCard.setClientId(clientId);
		clientBankCard.setCardNo(cardNo);
		if(clientBankCard.getId()==null) {
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "id必传!");
		}
		if(clientBankCard.getCardNo()==null) {
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "cardNo必传!");
		}
		if(clientBankCard.getCardType()==null) {
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "cardType必传!");
		}
		boolean success=clientBankCardService.defaultCard(clientBankCard);
		return new ObjectDataResponse<Boolean>(success);
	}

	/**
	 * 解绑卡
	 */
	@RequestMapping(value = "removeCard", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "解绑卡" , notes = "解绑卡")
	public ObjectDataResponse<Boolean> removeCard(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId,@RequestBody ClientBankCard clientBankCard){
		clientBankCard.setClientId(clientId);
		if(clientBankCard.getId()==null) {
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "id必传!");
		}
		if(clientBankCard.getCardNo()==null) {
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "cardNo必传!");
		}
		if(clientBankCard.getCardType()==null) {
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "cardType必传!");
		}
		boolean success=clientBankCardService.removeCard(clientBankCard);
		return new ObjectDataResponse<Boolean>(success);
	}
}
